import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
/**
 * 使用说明： <app-search-condition [options]="params" (deleteEvent)="deleteEvent($event)"></app-search-condition>
 * options: 筛选的结果JSON对象
 *      options = {
            'chain': [
                { value: '1', label: '侦查' },
                { value: '2', label: '工具制作' },
            ],
            'event': [
                { value: '6', label: '网络窃鱼事件' },
                { value: '7', label: '系统入侵事件' },
            ],
            'serverity': [
                { value: '1', label: '低' },
                { value: '2', label: '高' },
            ],
        };
 *  deleteEvent: 删除筛选的条件，触发事件。参数为：
        {
            options: 删除后的options值,
            key: 当前操作的options的key值
        }
 */
@Component({
    selector: 'app-search-condition',
    templateUrl: './search-condition.component.html',
    styleUrls: ['./search-condition.component.styl']
})
export class SearchConditionComponent implements OnInit {
    @Input() options: any = {};
    @Output() deleteEvent = new EventEmitter();

    keys = [];  // options的key值列表
    constructor() {
    }

    ngOnInit() {
        // this.options = {
        //     'chain': [
        //         { value: '1', label: '侦查' },
        //         { value: '2', label: '工具制作' },
        //     ],
        //     'event': [
        //         { value: '6', label: '网络窃鱼事件' },
        //         { value: '7', label: '系统入侵事件' },
        //     ],
        //     'serverity': [
        //         { value: '1', label: '低' },
        //         { value: '2', label: '高' },
        //     ],
        // };

        this.keys = this.getObjectKeys(this.options);
    }

    getObjectKeys(obj: Object) {
        return Object.keys(obj);
    }
    // 删除单个选项
    delete(option: any, key: string) {
        const index = this.options[key].indexOf(option);
        this.options[key][index].checked = false;
        this.options[key].splice(index, 1);
        this.options[key] = [...this.options[key]];
        console.log('app-search-condition结果列表：' + this.options);
        this.deleteEvent.emit({
            options: this.options,
            key: key
        });
    }
    // 删除所有选项
    deleteAll() {
        this.keys.forEach(key => {
            this.options[key].forEach(option => {
                option.checked = false; // 选项全部置为false
            });
            this.options[key] = [];
        });
        this.deleteEvent.emit({
            options: this.options,
            key: ''
        });
    }

}
